package com.android.common.flutter;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.android.common.BuildConfig;
import com.android.common.R;
import com.android.common.mvp.model.CodeMsgBean;
import com.android.common.constant.Constant;
import com.android.common.constant.IntentKey;
import com.android.common.constant.MethodNameConstant;
import com.android.common.mvp.model.WebViewParameter;
import com.android.common.utils.GsonUtil;
import com.android.common.utils.Router;
import com.android.common.utils.WebViewJsBridge;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * 用于传递方法调用（method invocation）（双向有返回值）
 */
public class BaseMethodChannelCallHandler implements MethodChannel.MethodCallHandler {
   @Override
   public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
      switch (call.method){
         case MethodNameConstant.METHOD_NAME_NATIVE_WEB_VIEW:
            WebViewParameter webViewParameter = GsonUtil.StringToObject(call.arguments(),WebViewParameter.class);
            if (TextUtils.isEmpty(webViewParameter.getUrl())){
               Toast.makeText(Constant.GLOBAL_APPLICATION,Constant.GLOBAL_APPLICATION.getString(R.string.url_not_empty),Toast.LENGTH_SHORT).show();
               return;
            }
            Bundle webViewbundle = new Bundle();
            webViewbundle.putBoolean(Constant.TOOLBAR_KEY,webViewParameter.getToolbar());
            webViewbundle.putParcelable(IntentKey.JS_BRIDGE_KEY,new WebViewJsBridge());
            webViewbundle.putString(IntentKey.URL_KEY,webViewParameter.getUrl());
            Router.getInstance().startActivity(Constant.GLOBAL_APPLICATION, webViewbundle, BuildConfig.WEB_VIEW);
            result.success(GsonUtil.ObjectToString(CodeMsgBean.success()));
            break;
         case MethodNameConstant.METHOD_NAME_NATIVE_FUNCTION_TEST:
            Router.getInstance().startActivity(Constant.GLOBAL_APPLICATION, null, BuildConfig.FUNCTION_TEST);
            result.success(GsonUtil.ObjectToString(CodeMsgBean.success()));
            break;
         default:
            Toast.makeText(Constant.GLOBAL_APPLICATION,Constant.GLOBAL_APPLICATION.getString(R.string.not_implemented),Toast.LENGTH_SHORT).show();
            result.notImplemented();
      }
   }
}
